import json
import re
import sqlite3

file = open('./phrases-dict.js', mode='r', encoding='utf8')
text = file.read()
res = re.search(r'module.exports\s+=\s+(\{[\s\S]*\})', text).group(1)
res = re.sub('\'', '\"', res)
json_dict = dict(json.loads(res, encoding='utf8'))

connection = sqlite3.connect('database.db')
cursor = connection.cursor()
cursor.execute('''
    CREATE TABLE IF NOT EXISTS sentence (
        id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
        words TEXT NOT NULL UNIQUE ,
        spell TEXT NOT NULL
    )
''')

for key, value in json_dict.items():
    sql = '''
        REPLACE INTO sentence (words, spell) VALUES (?, ?)
    '''
    cursor.execute(sql, (key, json.dumps(value, ensure_ascii=False)))
cursor.close()
connection.commit()
connection.close()
print('sentence 更新完成')
